/**
 * Copyright 2009 Adam Ruggles.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.sapien.util.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.components.UIBean;
import org.apache.struts2.util.TextProviderHelper;

import com.opensymphony.xwork2.util.ValueStack;

/**
 * Pager Struts UI bean.
 * @author Adam
 * @version $Id: Pager.java 16 2009-04-12 05:05:02Z a.ruggles $
 * 
 * Created on Feb 24, 2009 at 6:10:07 PM 
 */
public class Pager extends UIBean {
	/**
	 * The default pager template.
	 */
	public static final String TEMPLATE = "pager";

	/**
	 * The url to fetch the page.
	 */
	protected String href;

	/**
	 * The word to use for the next label.
	 */
	protected String nextLabel;

	/**
	 * The current page.
	 */
	protected String page;

	/**
	 * The word to use for the previous label.
	 */
	protected String prevLabel;

	/**
	 * The number of results per page.
	 */
	protected String resultsPerPage;

	/**
	 * The total number of pages.
	 */
	protected String total;

	/**
	 * The total number of results.
	 */
	protected String totalResults;

	/**
	 * Constructs a pager.
	 * @param stack The xwork value stack.
	 * @param request The HTTP servlet request.
	 * @param response The HTTP servlet response.
	 */
	public Pager(final ValueStack stack, final HttpServletRequest request, final HttpServletResponse response) {
		super(stack, request, response);
	}

	/**
	 * {@inheritDoc}
	 * @see org.apache.struts2.components.UIBean#evaluateExtraParams()
	 */
	public void evaluateExtraParams() {
        super.evaluateExtraParams();
        if (href == null) {
        	addParameter("hasParams", Boolean.FALSE);
        	addParameter("hrefValue", "?page=");
        } else {
        	final String hrefValue = (String) findValue(href, String.class);
            addParameter("href", hrefValue);
            final Boolean hasParams = (hrefValue.indexOf('?') >= 0);
        	addParameter("hasParams", hasParams);
        	if (hasParams) {
        		addParameter("hrefValue", hrefValue + "&page=");
        	} else {
        		addParameter("hrefValue", hrefValue + "?page=");
        	}
        }
        if (nextLabel != null) {
            addParameter("nextLabel", TextProviderHelper.getText(nextLabel, nextLabel, stack));
        }
        if (page != null) {
            addParameter("page", findValue(page, Integer.class));
        }
        if (prevLabel != null) {
            addParameter("prevLabel", TextProviderHelper.getText(prevLabel, prevLabel, stack));
        }
        if (resultsPerPage != null) {
            addParameter("resultsPerPage", findValue(resultsPerPage, Integer.class));
        }
        if (total != null) {
            addParameter("total", findValue(total, Integer.class));
        }
        if (totalResults != null) {
            addParameter("totalResults", findValue(totalResults, Integer.class));
        }
	}

	/**
	 * {@inheritDoc}
	 * @see org.apache.struts2.components.UIBean#getDefaultTemplate()
	 */
	@Override
	protected String getDefaultTemplate() {
		return TEMPLATE;
	}

	/**
	 * Sets href.
	 * @param href the href to set.
	 */
	public void setHref(final String href) {
		this.href = href;
	}

	/**
	 * Sets nextLabel.
	 * @param nextLabel the nextLabel to set.
	 */
	public void setNextLabel(final String nextLabel) {
		this.nextLabel = nextLabel;
	}

	/**
	 * Sets page.
	 * @param page the page to set.
	 */
	public void setPage(final String page) {
		this.page = page;
	}

	/**
	 * Sets prevLabel.
	 * @param prevLabel the prevLabel to set.
	 */
	public void setPrevLabel(final String prevLabel) {
		this.prevLabel = prevLabel;
	}

	/**
	 * Sets resultsPerPage.
	 * @param resultsPerPage the resultsPerPage to set.
	 */
	public void setResultsPerPage(final String resultsPerPage) {
		this.resultsPerPage = resultsPerPage;
	}

	/**
	 * Sets total.
	 * @param total the total to set.
	 */
	public void setTotal(final String total) {
		this.total = total;
	}

	/**
	 * Sets totalResults.
	 * @param totalResults the totalResults to set.
	 */
	public void setTotalResults(final String totalResults) {
		this.totalResults = totalResults;
	}
}
